//
//  PGShareView.m
//  PhotographicGuide
//
//  Created by indream on 13-2-11.
//  Copyright (c) 2013年 indream. All rights reserved.
//

#import "PGShareView.h"
#import "PGArticleManager.h"
@implementation PGShareView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 193;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PGShareCell"];
    if (cell == nil) {
        cell = _isDetail?_tvDetailCell:_tvCell;
        //self.tvCell = nil;
        //self.tvDetailCell = nil;
    }
    if([PGArticleManager sharedManager].favors){
        [(PGShareCell*)cell favorNumLabel].text = [NSString stringWithFormat:@"( %d )",[PGArticleManager sharedManager].favors.count];
    }else{
        [(PGShareCell*)cell favorNumLabel].text = @"( 0 )";
    }
    return cell;
}
-(void)setIsFavorite:(BOOL)isFavorite{
    _isFavorite = isFavorite;
    if(_isFavorite==YES){
        [[PGArticleManager sharedManager]addFavor:_articleId];
        [[PGArticleManager sharedManager]addArticleFavor:_articleId];
    }else{
        [[PGArticleManager sharedManager]removeFavor:_articleId];
    }
    [_favoriteView setImage:[UIImage imageNamed:_isFavorite?@"icon_favorite_gray.png":@"icon_favorite.png"]];
    [_favoriteLabel setText:_isFavorite?@"已添加":@"添加到收藏"];
    
    [self.tableView reloadData];
}
-(void)setArticleId:(NSInteger)articleId{
    _articleId = articleId;
    if(_articleId>0){
        _article = [[PGArticleManager sharedManager]articleWithID:_articleId];
        _isFavorite = [[PGArticleManager sharedManager]favorByArticleId:_articleId];
        [_favoriteView setImage:[UIImage imageNamed:_isFavorite?@"icon_favorite_gray.png":@"icon_favorite.png"]];
        [_favoriteLabel setText:_isFavorite?@"已添加":@"添加到收藏"];
    }
    
}
- (IBAction)favoriteHandle:(id)sender {
    [self setIsFavorite:!_isFavorite];
}
@end
